package com.sunday.electric.receiver;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sunday.electric.R;
import com.sunday.electric.entity.Message;
import com.sunday.electric.entity.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

@SuppressLint("NewApi")
/**
 * 接收到信息，进行解析
 * @author pangzf
 * @blog:http://blog.csdn.net/pangzaifei/article/details/43023625
 * @github:https://github.com/pangzaifei/zfIMDemo
 * @qq:1660380990
 * @email:pzfpang451@163.com 
 */
public class PushMessageReceiver extends BroadcastReceiver {
    public static final String TAG = PushMessageReceiver.class.getSimpleName();
    public static final int NOTIFY_ID = 0x000;
    public static int mNewNum = 0;// 通知栏新消息条目，我只是用了一个全局变量，
    public static final String RESPONSE = "response";
    public static ArrayList<EventHandler> ehList = new ArrayList<EventHandler>();

    public static abstract interface EventHandler {
        public abstract void onMessage(Message message);

        public abstract void onBind(String method, int errorCode, String content);

        public abstract void onNotify(String title, String content);

        public abstract void onNetChange(boolean isNetConnected);

        public void onNewFriend(User u);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

    }

    @SuppressWarnings("deprecation")
    /**
     * 显示notification
     * @param message  显示的内容
     */
    private void showNotify(Message message) {
        // TODO Auto-generated method stub
        mNewNum++;
        // 更新通知栏

        int icon = R.drawable.notify_newmessage;
        CharSequence tickerText = message.getNick() + ":"
                + message.getMessage();
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        notification.flags = Notification.FLAG_NO_CLEAR;
        // 设置默认声音
        // notification.defaults |= Notification.DEFAULT_SOUND;
        // 设定震动(需加VIBRATE权限)
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.contentView = null;

  /*      Intent intent = new Intent(application, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(application, 0,
                intent, 0);
        notification.setLatestEventInfo(PushApplication.getInstance(),
                application.getSpUtil().getNick() + " (" + mNewNum + "条新消息)",
                tickerText, contentIntent);*/
        // 下面是4.0通知栏api
        // Bitmap headBm = BitmapFactory.decodeResource(
        // application.getResources(), PushApplication.heads[Integer
        // .parseInt(JsonUtil.getFromUserHead(message))]);
        // Notification.Builder mNotificationBuilder = new
        // Notification.Builder(application)
        // .setTicker(tickerText)
        // .setContentTitle(JsonUtil.getFromUserNick(message))
        // .setContentText(JsonUtil.getMsgContent(message))
        // .setSmallIcon(R.drawable.notify_newmessage)
        // .setLargeIcon(headBm).setWhen(System.currentTimeMillis())
        // .setContentIntent(contentIntent);
        // Notification n = mNotificationBuilder.getNotification();
        // n.flags |= Notification.FLAG_NO_CLEAR;
        //
        // n.defaults |= Notification.DEFAULT_VIBRATE;

     //   application.getNotificationManager().notify(NOTIFY_ID, notification);// 通知一下才会生效哦
    }

    /**
     * 处理登录结果
     * 
     * @param errorCode
     * @param content
     */
    private void paraseContent(final Context context, int errorCode,
            String content) {
        // TODO Auto-generated method stub
        if (errorCode == 0) {
            String appid = "";
            String channelid = "";
            String userid = "";

            try {
                JSONObject jsonContent = new JSONObject(content);
                JSONObject params = jsonContent
                        .getJSONObject("response_params");
                appid = params.getString("appid");
                channelid = params.getString("channel_id");
                userid = params.getString("user_id");
            } catch (JSONException e) {
               // L.e(TAG, "Parse bind json infos error: " + e);
            }
            // SharePreferenceUtil util = PushApplication.getInstance()
            // .getSpUtil();
            // User u = new User(userid, channelid, msg.getNick(),
            // headId, 0);
     /*       mSpUtil.setAppId(appid);
            mSpUtil.setChannelId(channelid);
            mSpUtil.setUserId(userid);*/
        } else {
       /*     if (NetUtil.isNetConnected(context)) {
                if (errorCode == 30607) {
                    T.showLong(context, "账号已过期，请重新登录");
                    // 跳转到重新登录的界面
                } else {
                    T.showLong(context, "启动失败，正在重试...");
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            PushManager.startWork(context,
                                    PushConstants.LOGIN_TYPE_API_KEY,
                                    PushApplication.API_KEY);
                        }
                    }, 2000);// 两秒后重新开始验证
                }
            } else {
                T.showLong(context, R.string.net_error_tip);
            }*/
        }
    }
}
